#include"RingQueue.hpp"
#include"Task.h"

int add(int x, int y)
{
    return x + y;
}

void* produce(void* args)
{
    RingQueue<Task>* prq = (RingQueue<Task>*)args;
    while(true)
    {
        int x = rand() % 10 + 1;
        int y = rand() % 10 + 1;
        Task t(x, y, add);
        prq->push(t);
        printf("生产(%p)：%d + %d = ???\n", pthread_self(), x, y);
    }
    return nullptr;
}
void* consume(void* args)
{
    RingQueue<Task>* prq = (RingQueue<Task>*)args;
    while(true)
    {
        sleep(1);
        Task t;
        prq->pop(&t);
        printf("消费(%p)：%d + %d = %d\n", pthread_self(), t.x(), t.y(), t());
    }
    return nullptr;
}
int main()
{
    srand((uint64_t)time(nullptr) ^ getpid());
    RingQueue<Task> rq;
    pthread_t c[3];
    pthread_t p[1];
    for(int i = 0; i < 3; ++i) pthread_create(c+i, nullptr, consume, (void*)&rq);
    for(int i = 0; i < 1; ++i) pthread_create(p+i, nullptr, produce, (void*)&rq);

    for(int i = 0; i < 3; ++i) pthread_join(c[i], nullptr);
    for(int i = 0; i < 1; ++i) pthread_join(p[i], nullptr);
    return 0;
}